<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/1/15
 * Time: 10:34
 */
namespace app\modules\bg\controllers;

use app\common\service\AutoSendMail;
use app\common\service\CacheRunning;
use app\common\service\KaiGuan;
use app\modules\setting\models\yjrwsz\SetYjrwszRwsz;
use yii\console\Controller;
use yii\helpers\ArrayHelper;

class AutomailController extends Controller
{
    public $running_key = "bg/automail/run";
    /**
     * 每日执行
     * 测试环境
     * cd /www/wwwroot/test_email_to_creator.vikoc.cn/api && php yii bg/automail/run  test_email_to_creator.vikoc.cn
     * 线上环境
     *
     命令：* * * * * cd /www/wwwroot/email_tk_goods_to_creator/api && php yii bg/automail/run email_to_creator.vikoc.cn 2>&1
     */
    public function actionRun(){
        $running_obj = new CacheRunning();
        if($running_obj->isRunning()){
            echo "其他进程正在执行，当前进程终止。";
            exit();
        }
        try{
            $running_obj->running();

            $obj_kg = new KaiGuan();
            //开启系统开关，才会执行。
            if($obj_kg->isRunningKg(KaiGuan::SYS_RUNNING)){
                $auto_obj = new AutoSendMail();
                //执行主体逻辑
                $auto_obj->run();
            }

            $running_obj->stop();
            echo "执行完毕";
            exit();
        }catch (\Throwable $e){
            $running_obj->stop();
            echo $e->getMessage();
            exit();
        }
    }
}